/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	深圳市国省检日常化监管系统         </p>
 * <p>创建者   :	hey 
 * 
 * <p>描   述  :   TunnelHazarServiceImpl.java for com.pengtu.gsj.service.impl.check.yh.bridgeTunnel    </p>
 * 
 * <p>最后修改 : $: Sep 18, 2017-9:53:00 AM v 1.0.0	 hey   $ </p>
 * 
*/

package com.pengtu.gsj.service.impl.check.yh.bridgeTunnel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.yh.TunnelRecordDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.yh.TunnelRecord;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.yh.bridgeTunnel.TunnelHazardService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;

/**
 * 
 * TunnelHazarServiceImpl
 * 
 * Sep 18, 2017 9:53:00 AM
 * 
 * @version 1.0.0
 * 
 */
@Service
@Transactional
public class TunnelHazardServiceImpl implements TunnelHazardService {
	@Autowired
	private TunnelRecordDao tunnelRecordDao;
	
	@Autowired
	private CheckTableService checkTableService;
	
	@Autowired
	private SystemManageService systemManageService;
	
	
	@Override
	public List<TunnelRecord> getTunnelRecordList(QueryFilter queryFilter){
		return tunnelRecordDao.find(queryFilter, "testYear", true);
	}
	
	@Override
	public Map<String, Object> getTunnelRecordMap(String startTime, String endTime, CheckTable checkTable, String tag)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		List<TunnelRecord> tunnelRecordList = new ArrayList<TunnelRecord>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		
		//取到所有文件
		List<Attachment> list = systemManageService.getAllCheckAttachments(startTime, endTime, tag);
		//现场检查路线的名称
		QueryFilter queryFilter = new QueryFilter();
		queryFilter.addFilter("testYear", startTime, MatchType.GE);
		queryFilter.addFilter("testYear", endTime, MatchType.LE);
		
		double total =0; 					//检查得分
		double percentage =0; 				//系统完善度
		double tableFull  = yearFull;		//满分
		int years[] = new int[systemManageService.getCheckYear(endTime, startTime, 1)];
		for (int i = 0; i < years.length; i++) {
			years[i] = Integer.parseInt(startTime)+i;
		}
		tunnelRecordList = this.getTunnelRecordList(queryFilter);
		for (TunnelRecord tunnelRecord : tunnelRecordList) {
			if (tunnelRecord.getGovernPate() != null) {
				total += tunnelRecord.getGovernPate() * yearFull /100;
			}
		}
		
		
		map.put("sum",  MathsUtils.formatDouble(total,1));
		//判断完善度
		percentage = MathsUtils.divDouble(total*100 , tableFull, 2) ;
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, true);
		map.put("percent", percentage);
		map.put("years", years);
		map.put("att", list);
		map.put("overView", tunnelRecordList);
		map.put("yearFull", yearFull);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		
		return map;
	}
}
